package com.xc.h05.controller.lishenao;

import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Free;
import com.xc.h05.service.lishenao.FreeService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class FreeController {
    @Autowired
    private FreeService freeService;
    @GetMapping("/free")
    public R selectAll(Integer pageNum, Integer pageSize,Free free){
        PageInfo<Free> freePageInfo = freeService.selectAll(pageNum, pageSize, free);
        return R.ok(freePageInfo);
    }

    @PostMapping("/addfree")
    public R add(@RequestBody Free free){
        int i = freeService.insert(free);
        if (i>0){
            return R.ok("添加成功");
        }else   {
            return R.error();
        }
    }

    @PutMapping("/updatefree")
    public R update(@RequestBody Free free){
        int i = freeService.update(free);
        if (i>0){
            return R.ok("修改成功");
        }else   {
            return R.error();
        }
    }

    @DeleteMapping("/deletefree")
    public R delete(Long id){
        int i = freeService.delete(id);
        if (i>0){
            return R.ok("删除成功");
        }else   {
            return R.error();
        }
    }

    @GetMapping("/freebyid")
    public R selectById(Long id){
        Free free = freeService.selectById(id);
        if (free!=null){
            return R.ok(free);
        }else   {
            return R.error();
        }
    }
}
